package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import service.OptionsService;
import service.SubjectsService;

import javax.annotation.Resource;

@Controller
public class OptionsController {

    @Resource
    private OptionsService optionsService;
    @Resource
    private SubjectsService subjectsService;

    @RequestMapping("show")
    public String show(Model model,
                       @RequestParam(value = "id",required = false) Integer id){
        subjectsService.getInfobyId(id);
        model.addAttribute("count",optionsService.getCount(id));
        model.addAttribute("options",optionsService.getById(id));
        model.addAttribute("showinfo",subjectsService.getInfobyId(id));
        return "show";
    }
    @RequestMapping(value = "show",method = RequestMethod.POST)
    public String show(Integer radio,Integer count){
        if(optionsService.UpdateVote(radio)){
            subjectsService.updateVote(count,radio);
            return "redirect:list";
        }else{
            return "show?id="+radio;
        }

    }



}
